<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/9
 * Time: 10:13
 */

namespace App\Http\Controllers\Account;


use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\TypeVail;
use App\Model\Account\Type;
use App\Tool\Error;
use Illuminate\Http\Request;

class TypeController extends Controller
{
    protected $type;

    public function __construct()
    {
        $this->type = new Type();
    }
    //类型列表
    public function typeList(Request $request)
    {
        $params = $request->all();
        $where = [];
        //类型名字
        if ($request->input('t_name')){
            $where[] = ['t_name', 'like', "%".$params['t_name']."%"];
        }
        $list = $this->type->typeList(env('PAGE_LIMIT_ADMIN'), $where);
        if (!$list) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }
    //添加类型
    public function typeAdd(Request $request)
    {
        $params = $request->all();
        $vali =TypeVail::addType($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $status = $this->type->typeAdd($params);
        if (!$status) {
            return $this->error(Error::ADD_ERROR);
        }
        return $this->success('ok', '添加成功');
    }

    //修改类型
    public function typeEdit(Request $request)
    {
        $params = $request->all();
        $vali = TypeVail::editType($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $status = $this->type->typeEdit($params);
        if (!$status) {
            return $this->error(Error::EDIT_ERROR);
        }
        return $this->success('ok', '修改成功');
    }

    //删除类型
    public function typeDel(Request $request)
    {
        $params = $request->all();
        $vali = TypeVail::delType($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $status = $this->type->typeDel($params);
        if ($status === -1) {
            return $this->error(Error::ERROR_DEL_HAVEDETAIL);
        }
        if (!$status) {
            return $this->error(Error::DEL_ERROR);
        }
        return $this->success('ok', '删除成功');
    }
    //获取所有分类
    public function getAllType()
    {
        $list = $this->type->getAllType();
        if (!$list) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }

}